package com.luyu.tenant;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.schema.Column;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class AuthTenantLineHandler implements TenantLineHandler {
    @Override
    public Expression getTenantId() {
        return new LongValue(TenantContextHolder.getTenantId());
    }


    /**
     * 通过上下文保存是否存在租户id，没有就忽略
     */
    @Override
    public boolean ignoreTable(String tableName) {
        // 没有上下文没有保存tenantId就忽略
        return ObjUtil.isNull(TenantContextHolder.getTenantId());
    }

    @Override
    public boolean ignoreInsert(List<Column> columns, String tenantIdColumn) {
        return false;
    }
}
